<template>
  <el-cascader
    size="mini"
    :options="depts"
    :props="{value: 'id',label:'name'}"
    class="inputW"
    separator="-"
    :value="value"
    @change="changedept"
  />
</template>

<script>
import { getDeptsList } from '@/api/department'
import { transListToTree } from '@/utils'
export default {
  components: {},
  props: {
    value: {
      type: Number,
      default: null // 不设默认值报错
    }
  },
  data() {
    return {
      depts: []
    }
  },
  created() {
    this.getDeptsList()
  },
  methods: {
    // 获取树组件数据
    async getDeptsList() {
      this.depts = transListToTree(await getDeptsList(), 0)
    },
    changedept(list) {
      if (list.length > 0) {
        // 上传数组的最后一位
        this.$emit('input', list[list.length - 1])
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>

<style scoped>

</style>
